package cn.zxh.improvement.common.core.context;

import io.vavr.control.Option;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

/**
 * 本地属性设置
 *
 * @author zxh
 * @since 2023-4-28 16:50:59
 */
public class DefaultEnvironmentConfig implements EnvironmentConfig {
    static final EnvironmentConfig INST = new DefaultEnvironmentConfig();

    Map<String, String> config = new HashMap<>();

    @Override
    public Option<String> getValue(String name) {
        return Option.of(config.get(name)).orElse(() -> Option.of(System.getProperty(name)));
    }

    @Override
    public void setValue(String name, String value) {
        config.put(name, value);
    }

    @Override
    public Map<String, String> filter(BiPredicate<String, Object> predicate) {
        return config.entrySet().parallelStream().filter(e -> predicate.test(e.getKey(), e.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
}
